package org.wzl.todolisthelper.config;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class MinioConfig {

    @Value("${minio-endpoint}")
    private String endpoint;

    @Value("${minio-accessKey}")
    private String accessKey;

    @Value("${minio-secretKey}")
    private String secretKey;

    @Value("${minio-bucket}")
    private String bucket;

    @Bean
    public MinioClient minioClient() {
        MinioClient build = MinioClient.builder()
                .endpoint(endpoint)
                .credentials(accessKey, secretKey)
                .build();
        try {
            boolean found = build.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
            if (!found) {
                log.info("minio bucket 正在创建..., bucket:{}", bucket);
                build.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
            } else {
                log.info("minio bucket 已创建..., bucket:{}", bucket);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return build;
    }

}
